package com.cn.longdistancebusstation.registerAndLogin;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cn.longdistancebusstation.MainActivity;
import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.annotation.LoginStatus;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.model.UserInfo;
import com.google.gson.Gson;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/17.
 */

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener{
    private Button btn_register;
    private EditText mPasswordEditText=null;
    private EditText mSurePwdEditText=null;
    private String error_msg = "";
    final int SUCCESS_REGIRSTER = 1;
    final int FAIL_REGIRSTER = 0;
    private Message msg;
    private String musername;
    private String strPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settingpws);
        getTitleTextView().setText("设置密码");
        musername=getIntent().getStringExtra("telephone");
        initview();
    }

    private void initview() {
        mPasswordEditText = (EditText) findViewById(R.id.register_set_pwd);
        mSurePwdEditText = (EditText) findViewById(R.id.register_sure_pwd);
        btn_register = (Button) findViewById(R.id.register_btn_register);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.register_btn_register:
                register();
                break;
        }
    }
    private void register(){
        strPassword=mPasswordEditText.getText().toString().trim();
        final String strSecondPassword=mSurePwdEditText.getText().toString().trim();
        if(strPassword.equals(" ")||strSecondPassword.equals(" ")){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        if(!strPassword.equals(strSecondPassword)){
            Toast.makeText(this,"两次输入的密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
            return;
        }
        if (checkPassword(strPassword)){
            //注册
            showHud();
            Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
            HttpService httpService=retrofit.create(HttpService.class);
            Call<Result<UserInfo>> call=httpService.register(musername,strPassword);
            call.enqueue(new Callback<Result<UserInfo>>() {
                @Override
                public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
                    hideHud();
                    Result result=response.body();
                    Boolean isSuccess=result.getHead().getSuccess();
                    UserInfo info=(UserInfo)result.getBody();
                    if(isSuccess){
                        Toast.makeText(SetPasswordActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                        login();
                        Intent intent = new Intent();
                        intent.setAction("exit_current_boundary");
                        sendBroadcast(intent);
                        finish();
                    }else{
                        Toast.makeText(SetPasswordActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
                    hideHud();
                    Toast.makeText(SetPasswordActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                }
            });
        }
        else{
            Toast.makeText(this,"设置的密码长度必须在6到18位之间",Toast.LENGTH_LONG).show();
        }
    }

    private void login() {
        Retrofit retrofit = new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService = retrofit.create(HttpService.class);
        Call<Result<UserInfo>> call = httpService.login(musername,strPassword);
        call.enqueue(new Callback<Result<UserInfo>>() {
            @Override
            public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
                hideHud();
                Gson gson = new Gson();
                Result result = response.body();
                Log.d("登录用户信息",result+"");
                Boolean isSuccess = result.getHead().getSuccess();
                UserInfo userInfo = (UserInfo) result.getBody();

                if (isSuccess) {
                    String id = userInfo.getId();
                    Log.d("用户id",id);
                    String integral=userInfo.getIntegral();
                    Log.d("积分",integral);
                    SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
                    editor.putString("telephone", musername);
                    editor.putString("password", strPassword);
//                    editor.putString("integral",integral);
                    editor.commit();
                    //
                    GlobalVariable.setUserID(id);
                    GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGIN);

                } else {
                    GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGOUT);
                    Toast.makeText(SetPasswordActivity.this, result.getHead().getMsg(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
                hideHud();
                Toast.makeText(SetPasswordActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void showMsg(String msg){
        AlertDialog.Builder bd = new AlertDialog.Builder(SetPasswordActivity.this);
        bd.setTitle("提示");
        bd.setMessage(msg);
        bd.setNegativeButton("确定", null);
        bd.show();
    }

    private Boolean checkPassword(String content){
        String telRegex = "^[a-zA-Z0-9]{6,16}$";
        return content.matches(telRegex);
    }

}
